/// Creator:      gaoruishan
/// CreatDate:    2025-07-18
/// Description:  任务总览
Class Nur.MNISV3.Service.ExecuteSummary Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 任务总览查询
/// w ##class(Nur.MNISV3.Service.ExecuteSummary).GetTaskDataV4("26","2025-07-21","00:00","2025-07-21","23:59","3","20665","8","2","","{""single"":""1"",""multiple"":""""}")
ClassMethod GetTaskDataV4(episodeId, fromDate, fromTime, toDate, toTime, wardId, userId, locId, hospitalId, groupId, screenParts, extras) As %String
{
    n (episodeId, fromDate,fromTime, toDate,toTime, wardId, userId, locId, hospitalId,groupId,screenParts, extras,%session)
    s $zt="errGetTaskDataV4"
    s ^GRS("GetTaskDataV4",episodeId_"-")=$lb(episodeId, fromDate,fromTime, toDate,toTime, wardId, userId, locId, hospitalId,groupId,screenParts, extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s taskItems=..GetTaskItems(wardId,locId)
    s interventionItems=..GetInterventionItems(hospitalId)
    s ComConfig=..GetComConfig(hospitalId,wardId)
    s patInfoList=##class(%ListOfDataTypes).%New()  
    i episodeId'="" {//单患者
        s patinfo=##class(%ArrayOfDataTypes).%New()
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        s taskCount=0
        s taskData=..GetTaskDataObj(episodeId,fromDate,fromTime,toDate,toTime, wardId, userId, locId, hospitalId,groupId,.taskCount,screenParts) 
        d patinfo.SetAt($g(taskData),"taskData") 
        d patinfo.SetAt($g(taskCount),"taskCount") 
        d patInfoList.Insert(patinfo)
    }else {
        //调用纯净版患者列表，
        s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d patListObj.Execute(wardId,userId,"Ward",locId)
        while(patListObj.Next()){
            s patinfo=##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
            s episodeId=patListObj.GetDataByName("episodeId")
            s taskData=##class(%ListOfDataTypes).%New()
            d patinfo.SetAt($g(taskData),"taskData")
            //基本患者信息
            s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
            d patInfoList.Insert(patinfo)
        }
    }
     
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(taskItems),"taskItems")
    d json.SetAt($g(interventionItems),"interventionItems")
    d json.SetAt($g(ComConfig),"ComConfig")
    i Status="-1" s ^GRS("errlist",$h)=##class(Nur.MNISV3.Common.JSON).Encode(json)
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetTaskDataV4
    s Msg=$ze
    s ^GRS("errlist",$h)=Msg
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

ClassMethod GetTaskDataObj(episodeId, fromDate, fromTime, toDate, toTime, wardId, userId, locId, hospId, groupId, taskCount, screenParts)
{
    n (episodeId, fromDate, fromTime, toDate, toTime, wardId, userId, locId, hospId,groupId, taskCount, screenParts)
    s taskItems=..GetTaskItems(wardId,locId)
    s taskDataObj=##class(%ListOfDataTypes).%New()
    i fromTime="" s fromTime="00:00"
    i toTime="" s toTime="23:59"
    //固定格式:用户ID^科室ID^病区ID^院区ID^安全组ID
    s session=userId_"^"_locId_"^"_wardId_"^"_hospId_"^"_groupId
    s len=taskItems.Count()
    f i=1:1:len {
        s taskItemObj=taskItems.GetAt(i)
        s taskItemIdF=taskItemObj.GetAt("itemId")
        s taskItemDesc=taskItemObj.GetAt("desc")
        //continue:($g(taskItemId)'="")&&(taskItemId'=taskItemIdF)
        i (taskItemIdF="tabsDoctorOrderCzzl") { //治疗处置单
            s orders=##class(%ListOfDataTypes).%New()
            s taskTypeDataCount=0
            s rawObj=##class(Nur.MNISV3.Service.Order).GetOrdersBySheetCode(episodeId,"HLZLD",groupId,fromDate,fromTime,toDate,toTime,hospId,wardId,locId,"true","false","A","false","false","false","false","false","false")
            //s rawObj=##class(Nur.NIS.Service.OrderExcute.QueryOrder).GetOrders(episodeId,"HLZLD",groupId,fromDate,fromTime,toDate,toTime,hospId,wardId,locId,"true","false","A","false","false","false","false","false","false")
            //w rawObj,!
            s rawObj=##class(Nur.JSON).Decode(rawObj).GetAt("orders")
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                s oeoreId=$s($isobject(rawDataObj):rawDataObj.GetAt("ID"),1:rawDataObj)
                s oew=+oeoreId,chl=$p(oeoreId,"||",2)
                s arcimId=$p($g(^OEORD(oew,"I",chl,1)),"^",2)
                continue:(arcimId="")
                //医嘱基本信息
                d ##class(Nur.MNISV3.Service.Order).GetBasicInfo(oeoreId,rawDataObj)
                s taskTypeDataCount=$i(taskTypeDataCount)
                d orders.Insert(rawDataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(orders,"orders")
            d taskDataObj.Insert(taskTypeDataObj)
        } elseif (taskItemIdF="tabsDoctorOrderExe") { //医嘱执行-未执行
            s orders=##class(%ListOfDataTypes).%New()
            s taskTypeDataCount=0
            s rawObj=##class(Nur.MNISV3.Service.Order).GetOrdersBySheetCode(episodeId,"WZX",groupId,fromDate,fromTime,toDate,toTime,hospId,wardId,locId,"true","false","A","false","false","false","false","false","false")
            //s rawObj=##class(Nur.NIS.Service.OrderExcute.QueryOrder).GetOrders(episodeId,"HLZLD",groupId,fromDate,fromTime,toDate,toTime,hospId,wardId,locId,"true","false","A","false","false","false","false","false","false")
            s rawObj=##class(Nur.JSON).Decode(rawObj).GetAt("orders")
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                s oeoreId=$s($isobject(rawDataObj):rawDataObj.GetAt("ID"),1:rawDataObj)
                s oew=+oeoreId,chl=$p(oeoreId,"||",2)
                s arcimId=$p($g(^OEORD(oew,"I",chl,1)),"^",2)
                continue:(arcimId="")
                //医嘱基本信息
                d ##class(Nur.MNISV3.Service.Order).GetBasicInfo(oeoreId,rawDataObj)
                s taskTypeDataCount=$i(taskTypeDataCount)
                d orders.Insert(rawDataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(orders,"orders")
            d taskDataObj.Insert(taskTypeDataObj)
            
        }elseif (taskItemIdF="tabsEduTask") { //健康宣教
            s eduTasks=##class(%ListOfDataTypes).%New()
            s taskTypeDataCount=0
            k contentArr
            s rawObjParent=##class(Nur.NIS.Service.NursingTask.TaskController).UpdateEduTaskAndReturnList(episodeId,wardId,userId,locId,hospId,"",fromDate,toDate)
            //s rawObj=rawObjParent.GetAt("taskList")
            s rawObj=rawObjParent.GetAt("rtnData")
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                //s subjectId=rawDataObj.GetAt("subjectIds")
                //s subjectId=$$$ToJson(subjectId)
                s subjectId=rawDataObj.GetAt("subjectId")
                continue:(subjectId="")
                i ('$d(contentArr(subjectId))) {
                    s contentObj=##class(Nur.NIS.Service.Education2.Setting).GetEduContentsByIds("["_subjectId_"]").GetAt(1)
                    s contentArr(subjectId)=contentObj
                } else {
                    s contentObj=$g(contentArr(subjectId))
                }
                s taskTypeDataCount=$i(taskTypeDataCount)
                //continue:(countFlag)
                s dataObj=##class(Nur.Tools).RapidSubset("planDate^taskId",rawDataObj)
                s subjects=##class(%ListOfDataTypes).%New()
                s subject=##class(Nur.Tools).RapidSubset("title^content",contentObj)
                d subjects.Insert(subject)
                s ^GRS(11,$h)=subjectId
                s subjectIds=##class(Nur.Tools).GetOptionsAsList(subjectId,",")
                d dataObj.SetAt(subjects,"subjects")
                d dataObj.SetAt(subjectIds,"subjectIds")
                d eduTasks.Insert(dataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(eduTasks,"educationTasks")
            d taskDataObj.Insert(taskTypeDataObj)
        }elseif (taskItemIdF="tabsNurseOrderExe") { //护理计划
            s nurTasks=##class(%ListOfDataTypes).%New()
            s taskTypeDataCount=0
            s resSet=##class(%ResultSet).%New("Nur.NIS.Service.NursingPlan.TaskRecord:GetTaskList")
            d resSet.Execute(episodeId, fromDate_" "_fromTime,toDate_" "_toTime, 0,"")
            s rawObj=##class(Nur.Tools).ResultSetToList(resSet)
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                s itmId=rawDataObj.GetAt("interventionTypeId")
                //continue:(itmId="")||('$d(itmArr(itmId)))  interventionDR  tkStatus  recordId  emrRecordId
                s taskTypeDataCount=$i(taskTypeDataCount)
                //continue:(countFlag)
                s dataObj=##class(Nur.Tools).RapidSubset("interventionTypeId,interventionTypeName,planDateTime,executeItemName,itemInfo,executeNote,freqName"
                    _",executeUserName,executeDateTime,insertDateTime,stopDateTime,transferDateTime"
                    _",executeContent,tkStatusName,recordId,interventionDR,emrRecordId"
                    ,rawDataObj)
                d nurTasks.Insert(dataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(nurTasks,"nursingTasks")
            d taskDataObj.Insert(taskTypeDataObj)
        }elseif (taskItemIdF="tabsTasksAssess") { //评估任务
            s emrTasks=##class(%ListOfDataTypes).%New()
            s taskTypeDataCount=0
            k itmArr
            s itmObj=##class(Nur.NIS.Service.NursingTask.TaskOptions).GetTabTitles(hospId,wardId,2,1)
            s itmLen=itmObj.Count()
            f itmInd=1:1:itmLen {
                s itmDataObj=itmObj.GetAt(itmInd)
                s itmId=itmDataObj.GetAt("itemId")
                continue:(itmId="")
                s itmArr(itmId)=""
            }
            s parr=##class(Nur.Tools).RapidAssembly(
                "EpisodeIDs^WardID^UserId^LocID^HospDR^GroupID^StartDate^StartTime^EndDate^EndTime^TaskType^ItemId",
                episodeId,wardId,userId,locId,hospId,groupId,fromDate,fromTime,toDate,toTime,2,"")
            try {
                s rawObj=##class(Nur.NIS.Service.NursingTask.TaskController).GetNursingTaskRecordByStr(parr)
            } catch {
                s rawObj=""
            }
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                s itmId=rawDataObj.GetAt("itemId")
                continue:(itmId="")||('$d(itmArr(itmId)))
                s taskTypeDataCount=$i(taskTypeDataCount)
                d emrTasks.Insert(rawDataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(emrTasks,"NurEmrTasks")
            d taskDataObj.Insert(taskTypeDataObj)
        }elseif (taskItemIdF="tabsTasksSign") { //体征任务
            s obsTasks=##class(%ListOfDataTypes).%New()
            k itmArr
            s itmObj=##class(Nur.NIS.Service.NursingTask.TaskOptions).GetTabTitles(hospId,wardId,1,1)
            s itmLen=itmObj.Count()
            f itmInd=1:1:itmLen {
                s itmDataObj=itmObj.GetAt(itmInd)
                s itmId=itmDataObj.GetAt("itemId")
                continue:(itmId="")
                s itmArr(itmId)=""
            }
            s parr=##class(Nur.Tools).RapidAssembly(
                "EpisodeIDs^WardID^UserId^LocID^HospDR^GroupID^StartDate^StartTime^EndDate^EndTime^TaskType^ItemId",
                episodeId, wardId,userId,locId,hospId,groupId,fromDate,fromTime,toDate,toTime,1,"")
            s rawObj=##class(Nur.NIS.Service.NursingTask.TaskController).GetNursingTaskRecordByStr(parr)
            s rawLen=rawObj.Count()
            f rawInd=1:1:rawLen {
                s rawDataObj=rawObj.GetAt(rawInd)
                s itmId=rawDataObj.GetAt("itemId")
                continue:(itmId="")||('$d(itmArr(itmId)))
                s taskTypeDataCount=$i(taskTypeDataCount)
                s dataObj=##class(Nur.Tools).RapidSubset("exeDate^exeTime^itemCode^itemId^itemName^itemValue",rawDataObj )
                //continue:(countFlag)
                d obsTasks.Insert(dataObj)
            }
            s taskCount=taskCount+taskTypeDataCount
            s taskTypeDataObj=##class(%ArrayOfDataTypes).%New()
            d taskTypeDataObj.SetAt(taskItemIdF,"itemId")
            d taskTypeDataObj.SetAt(taskItemDesc,"itemDesc")
            d taskTypeDataObj.SetAt(taskTypeDataCount,"count")
            d taskTypeDataObj.SetAt(obsTasks,"obsTasks")
            d taskDataObj.Insert(taskTypeDataObj)
        }
    }
    q taskDataObj
}

ClassMethod GetOrders(episodeId, sheetCode, fromDate, fromTime, toDate, toTime, countFlag, ByRef error, session = "") As %String
{
    n (episodeId,sheetCode,fromDate,fromTime,toDate,toTime,countFlag,error,session,%session)
    i 'countFlag {
        s orders=##class(Nur.MNISV3.Service.Order).GetTaskViewAsList(episodeId,sheetCode,$zd(fromDate,3),$zt(fromTime,2),$zd(toDate,3),$zt(toTime,2),.error,session)
    }else {
        s orders=##class(Nur.MNISV3.Service.Order).GetTaskViewAsIdList(episodeId,sheetCode,$zd(fromDate,3),$zt(fromTime,2),$zd(toDate,3),$zt(toTime,2),.error,session) 
    }
    q orders
}

ClassMethod GetInterventionItems(hospId) As %String
{
    n (hospId)
    s resultObj=##class(%ResultSet).%New("Nur.NIS.Service.NursingPlan.InterventionSetting:FindInterventionType")
    d resultObj.Execute("",hospId)
    q ##class(Nur.Tools).ResultSetToList(resultObj)
}

ClassMethod GetTaskItems(wardId, locId) As %String
{
    n (locId,wardId)
    q ##class(Nur.NIS.Service.NursingTask.TaskOptions).GetAllTabTitles(wardId,locId,1)
}

ClassMethod GetComConfig(hospId, wardId) As %String
{
    n (hospId,wardId,%session)
    s config=##class(%ArrayOfDataTypes).%New()
    s timeline=##class(%ListOfDataTypes).%New()
    s sortStartTime=..GetProperty(hospId,wardId,"sortStartTime")
    s sortEndTime=..GetProperty(hospId,wardId,"sortEndTime")
    s sortNo=..GetProperty(hospId,wardId,"sortEndTime")
    i (sortNo=1) {
        s sttime=sortStartTime
        s edtime=sortEndTime
    } else {
        s sttime=sortEndTime
        s edtime=sortStartTime
    }
    s sthour=$p(sttime,":")
    s edhour=$p(edtime,":")
    i (sthour>edhour) s increment=-1
    e  s increment=1
    f h24=1:1:24 {
        s hour=sthour+(increment*(h24-1))
        i (hour>23) s hour=hour-24
        i (hour<0) s hour=hour+24
        i ($l(hour)=1) s hour="0"_hour
        s hour=hour_":00"
        d timeline.Insert(hour)
    }
    d config.SetAt(timeline,"timeline")
    q config
}

ClassMethod GetProperty(hospId, wardId, code) As %String
{
    n (hospId,wardId,code,%session)
    s obj=##class(Nur.NIS.Service.NursingTask.TaskOptions).getSeleteNursingTask(hospId,wardId,code)
    q:('$isobject(obj)) ""
    s value=obj.value
    d obj.%Close()
    q value
}

}
